import axios from "axios"
import router from "@/router"

// 如果使用 mock.js 需要将 baseURL 的字符串修改为一个 /
const instance = axios.create({
    baseURL: "http://192.168.1.29:8080/api/private/v1",
    timeout: 60000,
})
// q请求拦截器
instance.interceptors.request.use(
    function (config) {
        config.headers.Authorization = window.localStorage.getItem("token")
        return config
    },
    function (error) {
        return Promise.reject(error)
    }
)
// 响应拦截器
// instance.interceptors.response.use(
//     function(response) {
//         return response.data;
//     },
//     function(error) {
//         return Promise.reject(error);
//     }
// );

export function ajax(options) {
    const { url, method = "GET", data = {} } = options

    if (method.toUpperCase() === "GET") {
        return instance.get(url, { params: data })
    }

    if (method.toUpperCase() === "POST") {
        return instance.post(url, data)
    }
    if (method.toUpperCase() === "DELETE") {
        return instance.delete(url, data)
    }
    if (method.toUpperCase() === "PUT") {
        return instance.put(url, data)
    }
    if (method.toUpperCase() === "DELETE") {
        return instance.delete(url, data);
    }
    if (method.toUpperCase() === "PUT") {
        return instance.put(url, data);
    }
}